package com.dsteiner.widgets.client;

import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.Widgets.TextInput;
import com.dsteiner.widgets.client.controller.PrefilledTextBoxController;
import com.dsteiner.widgets.client.model.PrefilledTextBoxModel;
import com.dsteiner.widgets.client.resources.WidgetsResources;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;


public class PrefilledTextInput extends LazyWidget{
	private PrefilledTextBoxController prefilledTextBoxController;
	private final PrefilledTextBoxModel prefilledTextBoxModel;
	private TextInput textInput;
	
	public PrefilledTextInput(PrefilledTextBoxModel prefilledTextBoxModel, TextInput textInput) {
		this(prefilledTextBoxModel, textInput, new PrefilledTextBoxController());
	}
	
	public PrefilledTextInput(PrefilledTextBoxModel prefilledTextBoxModel, TextInput textInput, 
			PrefilledTextBoxController prefilledTextBoxController) {
		this.prefilledTextBoxController = prefilledTextBoxController;
		this.prefilledTextBoxModel = prefilledTextBoxModel;
		this.textInput = textInput;
		setStyleName(WidgetsResources.INSTANCE.css().prefilledTextInput());
	}
	
	@Override
	public Widget createContentWidget() {
//		VerticalPanel mainPanel = new VerticalPanel();
		FlowPanel mainPanel = new FlowPanel();
		HTMLExt defaultTextHtml = new HTMLExt();
		HTMLExt countHtml = new HTMLExt();
		
		mainPanel.add((Widget)textInput);
		mainPanel.add(defaultTextHtml);
		mainPanel.add(countHtml);
		
		prefilledTextBoxController.registerPrefilledTextBox(this);
		prefilledTextBoxController.registerTextBox(textInput);
		prefilledTextBoxController.registerPrefilledTextBoxModel(prefilledTextBoxModel);
		prefilledTextBoxController.registerDefaultTextHtml(defaultTextHtml);
		prefilledTextBoxController.registerCountHtml(countHtml);
		
		textInput.addStyleName(WidgetsResources.INSTANCE.css().textInput());
		textInput.addStyleName(WidgetsResources.INSTANCE.cssBoxSizing().bb());
		countHtml.addStyleName(WidgetsResources.INSTANCE.css().countHtml());
		mainPanel.setSize("100%", "100%");
		
		return mainPanel;
	}
	
	public void setTabIndex(int index){
		textInput.setTabIndex(index);
	}
	public int getTabIndex(){
		return textInput.getTabIndex();
	}
	public void setName(String name){
		textInput.setName(name);
	}
}